'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms
Imports System.Net

Public Class dodo
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String
            Dim Cost As String = ""
            Dim PeakTotal As Integer
            Dim OffpeakTotal As Integer

            'login
            Dim strTemp As String
            Dim POSTData As String = "txtUserID=" & ConfigurationData.Username & "&txtUserPWD=" & ConfigurationData.Password & "&imageField2.x=34&imageField2.y=13"
            strTemp = ISPPost("https://secure.dodo.com.au/DodoAccountManagement/Login/Login.aspx", POSTData, CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If strTemp.Contains("Login Failed") = True Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            'usage
            strHTMLData = ISPGet("https://secure.dodo.com.au/DodoAccountManagement/usage.aspx", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            'logout
            ISPGet("https://secure.dodo.com.au/DodoAccountManagement/login/logout.aspx", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            '##########################################################

            'process response
            If ConfigurationData.PeakUnlimited = True Then
                ISPReturnData.PeakDownload = CSng(Val(ExtractText(strHTMLData, "Total:", 0, "<b>", "</b>")))
                ISPReturnData.PeakUpload = CSng(Val(ExtractText(strHTMLData, "Total:", 17, "<b>", "</b>")))
            Else
                ISPReturnData.PeakDownload = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOnPeakDownload", 0, ">", "<")))
                ISPReturnData.PeakUpload = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOnPeakUploaded", 0, ">", "<")))

                PeakTotal = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOnPeakTotal", 0, "<b>", "<")))

                ISPReturnData.OffpeakDownload = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOffPeakDownLoaded", 0, ">", "<")))
                ISPReturnData.OffpeakUpload = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOffPeakUploaded", 0, ">", "<")))

                OffpeakTotal = CInt(Val(ExtractText(strHTMLData, "_ctl0_lblDSLDailyOffPeakTotal", 0, "<b>", "<")))

                'calculate cost
                Cost = "0"
                If PeakTotal > Val(ConfigurationData.PeakUsageLimit) Then
                    Cost = ((PeakTotal - Val(ConfigurationData.PeakUsageLimit)) * Val(ConfigurationData.ExcessRate)).ToString
                End If
                If OffpeakTotal > Val(ConfigurationData.OffpeakUsageLimit) Then
                    Cost = (Val(Cost) + ((OffpeakTotal - Val(ConfigurationData.OffpeakUsageLimit)) * Val(ConfigurationData.ExcessRate))).ToString
                End If

            End If

            ISPReturnData.LastUpdated = Now

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data
            If ConfigurationData.PeakUnlimited = True Then
                'downloads
                ISPReturnData.GraphItemList.Add(New GraphItem("Downloads " & ISPReturnData.PeakDownload & " (Unlimited)", ISPReturnData.PeakDownload, 0, False))

                'uploads
                ISPReturnData.GraphItemList.Add(New GraphItem("Uploads " & ISPReturnData.PeakUpload & " (Unlimited)", ISPReturnData.PeakUpload, 0, False))

                'days elapsed
                ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

                '##########################################################

                'create stats data
                ISPReturnData.TextItemList.Add(New TextItem("D/L Left :", "Unlimited"))
                ISPReturnData.TextItemList.Add(New TextItem("U/L Left :", "Unlimited"))
            Else
                'total usage peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((PeakTotal / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & PeakTotal & " MB)", PeakTotal, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))

                'total usage offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((OffpeakTotal / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & OffpeakTotal & " MB)", OffpeakTotal, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))

                'days elapsed
                ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

                '##########################################################

                'create stats data
                ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak D/L Left :", ISPReturnData.OffpeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Avg. Left :", ISPReturnData.OffpeakUsageRemainingPerDayDescription))

                'are we over max cost?
                If Val(Cost) > Val(ConfigurationData.ExcessMaxCharge) Then
                    Cost = ConfigurationData.ExcessMaxCharge
                End If

                If ISPReturnData.ShapedNow = False Then
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                End If

                ISPReturnData.TextItemList.Add(New TextItem("Excess Cost :", "$" & Cost))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
